package linkedlistdemo;

import java.util.Stack;

/**
 * @Author 12629
 * @Date 2022/3/12 12:19
 * @Description：
 */
class ListNode {
    public int val;
    public ListNode next;//类型是Node  null

    public ListNode(int val) {
        this.val = val;
    }
}
public class SingleLinkedList {

    public ListNode head;//为什么定义到这里
    public int usedSize;//记录当前链表中节点的个数

    /**
     * 先来了解一下链表的结构
     * 使用穷举的方式，创建链表
     */
    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        //int a = 10;
        node1.next = node2;//？
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
        this.usedSize = 5;
    }


    /**
     * 打印链表里面的元素
     */
    public void myToString(){
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 从指定的位置开始打印链表
     * @param newHead
     */
    public void myToString(ListNode newHead){
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null) {
            //equals
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //得到单链表的长度-》尝试不用我们定义的usedSize
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public int size2(){
       return this.usedSize;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
        }else {
            node.next = head;
            head = node;
        }
        this.usedSize++;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.head == null) {
            this.head = node;
        }else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            //cur.next == null
            cur.next = node;
        }
        this.usedSize++;
    }

    /**
     * 查找Index-1位置的节点的地址
     * @param index
     * @return
     */
    private ListNode searchIndex(int index) {
        int count = 0;
        ListNode cur = this.head;
        while (count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index < 0 || index > this.usedSize) {
            throw new RuntimeException("index不合法！");
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == this.usedSize) {
            addLast(data);
            return;
        }
        ListNode listNode = new ListNode(data);
        ListNode cur = searchIndex(index);
        listNode.next = cur.next;
        cur.next = listNode;
        this.usedSize++;//这里注意 该加要加  该减要减
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head == null) {
            return;
        }

        if(this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        ListNode prev = findPrevNode(key);
        if(prev == null) {
            throw new RuntimeException("不存在你要删除的节点！");
        }
        ListNode del = prev.next;
        prev.next = del.next;
        this.usedSize--;
    }

    /**
     * 找到关键字Key的前一个节点
     * @param key
     * @return 不存在 返回null 存在返回节点的地址
     */
    private ListNode findPrevNode(int key) {
        ListNode prev = this.head;
        while (prev.next != null) {
            if(prev.next.val == key) {
                return prev;
            }
            prev = prev.next;//????????
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(this.head == null) return;
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while (cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
                this.usedSize--;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(this.head.val == key) {
            this.head = this.head.next;
            this.usedSize--;
        }
    }

    /**
     * 清空链表
     */
    public void clear1(){
        this.head = null;
        this.usedSize = 0;
    }

    public void clear (){
        ListNode cur = this.head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
        this.usedSize = 0;
    }
    //shift+f6
    public ListNode reverseList() {
        if(this.head == null) {
            return null;
        }
        if(this.head.next == null) {
            return this.head;
        }
        //最起码有2个节点以上
        ListNode cur = this.head.next;
        this.head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = this.head;
            this.head = cur;
            cur = curNext;
        }
        return this.head;
    }

    /**
     * 找到链表的中间节点
     * @return
     */
    public ListNode middleNode() {
        ListNode fast = head;
        ListNode slow = head;
       while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    /**
     * 找到单链表 倒数第K个节点
     * 要去 只遍历链表一遍
     * @param k
     * @return
     */
    public ListNode findKthToTail(int k) {
        //k是否是合法的
        if(k <= 0 || head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;

        while (k-1 > 0) {
            if(fast.next == null) {
                return null;
            }
            fast = fast.next;
            /*if(fast == null) {
                return null;
            }*/
            k--;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }


    /**
     * 以X为基准 进行分割链表
     * @param pHead
     * @param x
     * @return
     */
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        if(pHead == null) {
            return null;
        }

        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = pHead;

        while (cur != null) {
            if(cur.val < x) {
                if(bs == null) {
                    bs = cur;
                    be = cur;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            }else {
                //是不是第一次插入
                if(as == null) {
                    as = cur;
                    ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs == null) {
            return as;
        }
        be.next = as;
        //检查最后一段是不是有数据，如果有，那么我们需要进行把最后一个节点的next置为空，否则会出现死循环
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }

    /**
     * 判断链表是不是回文链表
     * @return
     */
    public boolean chkPalindrome() {
        if(head == null) {
            return true;
        }
        if(head.next == null) {
            return true;
        }
        //1、找到中间节点
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2、slow已经到中间位置了,开始翻转
        ListNode cur = slow.next;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //3、slow已经到最后一个节点了，开始往回走
        while(head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    /**
     * 判断链表是否有环
     * @param head
     * @return
     */
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        //
        return false;
    }

    /**
     * 判断链表是否有环
     * @param head
     * @return
     */
    public boolean hasCycle2(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
               break;
            }
        }
        if(fast == null || fast.next == null) {
            return false;
        }
        return true;
    }

    /**
     * 求环的入口点
     * @param head
     * @return
     */
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;
        }

        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

    /**
     * 递归逆序打印单链表
     * @param head
     */
    public void printList(ListNode head) {
        if(head == null) {
            return;
        }
        if(head.next == null) {
            System.out.print(head.val+" ");
            return;
        }
        printList(head.next);
        System.out.print(head.val+" ");
    }

    public void printList2() {
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (!stack.empty()) {
            ListNode tmp = stack.pop();
            System.out.print(tmp.val+" ");
        }
        System.out.println();
    }
}
